import logging

logger = logging.getLogger('serial.consumer')


class WebSocketConnection(object):
    def __init__(self, websocket):
        self.socket = websocket
        self.current_order = None

    def is_close(self):
        return self.socket.is_closed()

    def msg_send(self, msg: str):
        try:
            if not self.is_close():
                self.socket.send(msg)
            else:
                self.current_order.is_socket_connected = False
                logger.warning('样本建模与检测线程: websocket未连接')
        except Exception as e:
            self.current_order.is_socket_connected = False
            logger.error(f'消息发送失败,websocket服务退出,{e}')

    def msg_read(self):
        try:
            msg = self.socket.wait()
            return msg
        except Exception as e:
            logger.error(f'读取失败, {e}')
